package service

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"wechat-moments_0926/internal/model"
	"wechat-moments_0926/internal/params"
)

func AddFeed(c *gin.Context) {
	var req params.AddFeedForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var feed model.Feed

	feed = model.Feed{
		UserId: req.UserId,
		Image:  req.Image,
		Text:   req.Text,
		Status: req.Status,
	}

	err := feed.AddFeed()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "发布失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "发布成功",
		"data":    feed,
	})
}

func ListFeed(c *gin.Context) {

	var feed model.Feed

	findFeed, err := feed.FindFeed()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "查询失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "查询成功",
		"data":    findFeed,
	})

}
